package com.canbot.u05.activity.customvoice;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;

import com.alibaba.fastjson.JSON;
import com.canbot.u05.R;
import com.canbot.u05.activity.BaseActivityNoTitle;
import com.canbot.u05.activity.customvoice.bean.AnswerBean;
import com.canbot.u05.activity.customvoice.bean.QuestionBean;
import com.canbot.u05.socket.MsgSendUtils;
import com.canbot.u05.socket.MsgType;
import com.canbot.u05.utils.PathConst;
import com.canbot.u05.utils.ToastTools;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 自定义对话问题设置
 *
 * @author  liu.heng
 * @date 2018/5/24
 */
public class CustomQuestionActivity extends BaseActivityNoTitle {

        private EditText mEditText;

        private List<QuestionBean> mQusetionBeans = new ArrayList<>();

        private final int ADD_ANSWER = 0;

        private final int CHANGE_ANSWER = 1;

        private int type;

        private String qusetion;

        private String oldQuestion;

        private AnswerBean mAnswerBean;

        private static Pattern SPACE_PATTERN = Pattern.compile("\\s*|\t|\r|\n");

        private static Pattern SYMBOLS_PATTERN = Pattern.compile("[`~!@#$^&*()+=|{}':;',\\\\[\\\\].<>/?~！@#￥……& amp;*（）——+|{}【】‘；：”“’。，、？|-]");


        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_custom_question);
                mEditText = (EditText) findViewById(R.id.my_edittext);
                mEditText.addTextChangedListener(watcher);
                final Intent intent = getIntent();
                mQusetionBeans = JSON.parseArray(intent.getStringExtra("data"), QuestionBean.class);
                type = intent.getIntExtra("type", -1);
                if (type == CHANGE_ANSWER) {
                        qusetion = oldQuestion = intent.getStringExtra("question_Str");
                        mAnswerBean = JSON.parseObject(intent.getStringExtra("answer"), AnswerBean.class);
                        mEditText.setText(qusetion);
                }
        }

        private TextWatcher watcher = new TextWatcher() {

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                        final int maxSize = 20;
                        if (s.length() > maxSize) {
                                mEditText.setText(s.subSequence(0, maxSize));
                                mEditText.setSelection(maxSize);
                                ToastTools.newInstance(CustomQuestionActivity.this).showText(getString(R.string.input_up_words7));

                        }
                }

                @Override
                public void beforeTextChanged(CharSequence s, int start, int count,
                                              int after) {

                }

                @Override
                public void afterTextChanged(Editable s) {

                }
        };

        /*下一步*/
        public void nextStep(View view) {
                if (!mEditText.getText().toString().isEmpty()) {
                        if (!mEditText.getText().toString().isEmpty()) {

                                //判断问题是否重复
                                if (repeatDetectionProblem(mEditText.getText().toString())) {
                                        if (type == CHANGE_ANSWER) {
                                                startActivity(new Intent(this, CustomAnswerActivity.class).
                                                        putExtra("type", 1)
                                                        .putExtra("question_Str", replaceStr(mEditText.getText().toString()))
                                                        .putExtra("oldquestion", oldQuestion)
                                                        .putExtra("answer", JSON.toJSONString(mAnswerBean)));
                                        }
                                        else {
                                                startActivity(new Intent(this, CustomAnswerActivity.class)
                                                        .putExtra("type", 0).putExtra("question_Str", replaceStr(mEditText.getText().toString())));
                                        }
                                        MsgSendUtils.sendStringMsg(MsgType.PLAY_SOUND, PathConst.CUSTOM_DIALOG_PAYH + "u05_dzhd_001.wav");
                                        MsgSendUtils.sendStringMsg(MsgType.EYE_MOTION, "34");
                                        MsgSendUtils.sendStringMsg(MsgType.ACTION, "14006");
                                }
                                else {
                                        ToastTools.newInstance(getApplicationContext()).showText(CustomQuestionActivity.this.getString(R.string.problem_Repetition));
                                }

                        }
                        else {
                                ToastTools.newInstance(getApplicationContext()).showText(CustomQuestionActivity.this.getString(R.string.problem_cannot_empty));
                        }

                }
                else {
                        ToastTools.newInstance(getApplicationContext()).showText(CustomQuestionActivity.this.getString(R.string.problem_cannot_empty));
                }

        }

        /**
         * 返回
         */
        public void back(View view) {
                finish();
        }

        private Boolean repeatDetectionProblem(String question) {

                if (mQusetionBeans != null) {
                        for (int i = 0; i < mQusetionBeans.size(); i++) {
                                if (mQusetionBeans.get(i).getQuestion().equals(question)) {
                                        //问题重复
                                        return false;
                                }
                        }
                }
                //问题无重复
                return true;
        }

        /**
         * 移除字符串中的特殊字符
         */
        private static String replaceStr(String str) {
                String question = "";
                if (str != null) {
                        Matcher m = SPACE_PATTERN.matcher(str);
                        question = m.replaceAll("");
                        Matcher q = SYMBOLS_PATTERN.matcher(question);
                        question = q.replaceAll("");
                }
                return question;
        }
}
